package org.lhh.server.controller;


import org.apache.commons.lang3.StringUtils;
import org.lhh.server.annotation.LogCatch;
import org.lhh.server.annotation.UserAuth;
import org.lhh.server.common.api.JSONResult;
import org.lhh.server.common.utils.CollectionUtils;
import org.lhh.server.core.codec.directory.RepositoryDirectoryCodec;
import org.lhh.server.core.repository.EtlRepository;
import org.pentaho.di.repository.LongObjectId;
import org.pentaho.di.repository.RepositoryDirectory;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/directory")
public class DirectoryController {


    @UserAuth
    @LogCatch(value = "资源目录-树")
    @GetMapping("/tree")
    public JSONResult tree() {
        return JSONResult.ok(getTree(EtlRepository.directoryTree()));
    }


    @UserAuth
    @LogCatch(value = "资源目录-新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JSONResult add(@RequestParam Long pId) {
        int i = 1;
        String name = "新建目录";
        while (EtlRepository.existDirectory(name + i, null, new LongObjectId(pId))) {
            i++;
        }
        RepositoryDirectory repositoryDirectory = new RepositoryDirectory();
        repositoryDirectory.setParent(EtlRepository.getDirectory(new LongObjectId(pId)));
        repositoryDirectory.setName(name + i);
        EtlRepository.saveDirectory(repositoryDirectory);
        return JSONResult.ok(RepositoryDirectoryCodec.encode(repositoryDirectory));
    }


    @UserAuth
    @LogCatch(value = "资源目录-重命名")
    @RequestMapping(value = "/rename", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult rename(@RequestParam(value = "id") Long id,
                             @RequestParam(value = "pId") Long pId,
                             @RequestParam(value = "name") String name) {
        if (StringUtils.isBlank(name)) {
            return JSONResult.errorMsg("名称不能为空");
        }
        if (EtlRepository.existDirectory(name, new LongObjectId(id), new LongObjectId(pId))) {
            return JSONResult.errorMsg("名称已存在");
        }
        EtlRepository.renameDirectory(name, new LongObjectId(id), new LongObjectId(pId));
        return JSONResult.ok(RepositoryDirectoryCodec.encode((RepositoryDirectory) EtlRepository.getDirectory(new LongObjectId(id))));
    }


    @UserAuth
    @LogCatch(value = "资源目录-删除")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JSONResult delete(@PathVariable Long id) {
        EtlRepository.removeDirectory(new LongObjectId(id));
        return JSONResult.ok();
    }

    @UserAuth
    @LogCatch(value = "资源目录-移动")
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult move(@RequestParam(value = "id") Long id,
                           @RequestParam(value = "targetId") Long targetId) {
        EtlRepository.moveDirectory(new LongObjectId(id), new LongObjectId(targetId));
        return JSONResult.ok();
    }

    private RepositoryDirectoryCodec getTree(RepositoryDirectoryInterface repositoryDirectoryInterface) {
        RepositoryDirectoryCodec repositoryDirectoryCodec = RepositoryDirectoryCodec.encode((RepositoryDirectory) repositoryDirectoryInterface);
        List<RepositoryDirectoryInterface> children = repositoryDirectoryInterface.getChildren();
        if (!CollectionUtils.isEmpty(children)) {
            List<RepositoryDirectoryCodec> repositoryDirectoryCodecs = new ArrayList<>();
            for (RepositoryDirectoryInterface repositoryDirectoryInterface_ : children) {
                repositoryDirectoryCodecs.add(getTree(repositoryDirectoryInterface_));
            }
            repositoryDirectoryCodec.setChildren(repositoryDirectoryCodecs);
        }
        return repositoryDirectoryCodec;
    }
}
